<?php
namespace App\Http\Controllers\Admin;
use App\Models\Article;
use App\Repositories\Contracts\ArticleInterface;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ArticleRequest;
use Illuminate\Support\Facades\Storage;

class ArticleController extends Controller
{
    protected $article;

    /**
     * 依赖注入
     * ArticleController constructor.
     * @param ArticleInterface $article
     */
    public function __construct(ArticleInterface $article){
        $this->article = $article;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){
        $date = $this->article->all(config('admin.gloables.perPage'));
        return view('admin.article.index',compact('date'));
    }

    public function search(Request $request){
        $date = $this->article->searchKeyWords($request->all(),config('admin.gloables.perPage'));
        return view('admin.article.index',compact('date'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.article.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ArticleRequest $request){
        $this->article->createInfo($request->except('_token'));
        return redirect('/admin/article');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id){
        $data = $this->article->showId($id);
        return view('admin.article.show',compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id){
        $data = $this->article->findById($id);
        return view('admin.article.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id){
//        dd($request->except('_token','_method'),$id);
        $this->article->updateId($request->except('_token','_method'),$id);
        return redirect('/admin/article/');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        return $this->article->destroyById($id);

    }

    public function upload(Request $request){
        $file = $request->file('upload');
        if( $file->isValid() ){
            // 原文件名
            $originaName = $file->getClientOriginalName();
            // 扩展名
            $ext = $file->getClientOriginalExtension();
            // 文件类型
            $type = $file->getClientMimeType();
            // 临时绝对路径
            $realPath = $file->getRealPath();
            $fileName = date('YmdHis').uniqid().$ext;
            $bool = Storage::disk('uploads')->put($fileName,file_get_contents($realPath));

        }
    }
}
